#!/bin/bash
# 初始化元数据-a集群
function initialize_cluster_metadata_a() {
  cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/ || exit
  bin/pulsar initialize-cluster-metadata \
    --cluster pulsar-service-1 \
    --zookeeper 10.91.37.47:2181,10.91.37.48:2181,10.91.37.49:2181 \
    --configuration-store 10.91.37.47:2181,10.91.37.48:2181,10.91.37.49:2181 \
    --web-service-url http://10.91.37.47:8080,10.91.37.48:8080,10.91.37.49:8080 \
    --web-service-url-tls https://10.91.37.47:8443,10.91.37.48:8443,10.91.37.49:8443 \
    --broker-service-url pulsar://10.91.37.47:6650,10.91.37.48:6650,10.91.37.49:6650 \
    --broker-service-url-tls pulsar+ssl://10.91.37.47:6651,10.91.37.48:6651,10.91.37.49:6651
  # 接着初始化bookkeeper集群：若出现提示输入Y/N：请输入Y
  echo "Y" | bin/bookkeeper shell metaformat
}

# 初始化元数据-b集群
function initialize_cluster_metadata_b() {
  cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/ || exit
  bin/pulsar initialize-cluster-metadata \
    --cluster pulsar-service-1 \
    --zookeeper 10.102.35.153:2181,10.102.35.154:2181,10.102.35.155:2181 \
    --configuration-store 10.102.35.153:2181,10.102.35.154:2181,10.102.35.155:2181 \
    --web-service-url http://10.102.35.153:8080,10.102.35.154:8080,10.102.35.155:8080 \
    --web-service-url-tls https://10.102.35.153:8443,10.102.35.154:8443,10.102.35.155:8443 \
    --broker-service-url pulsar://10.102.35.153:6650,10.102.35.154:6650,10.102.35.155:6650 \
    --broker-service-url-tls pulsar+ssl://10.102.35.153:6651,10.102.35.154:6651,10.102.35.155:6651
  # 接着初始化bookkeeper集群：若出现提示输入Y/N：请输入Y
  echo "Y" | bin/bookkeeper shell metaformat
}

# start bookie
function start_bookie() {
  cd /app/pmapp/data/rongshu/apache-pulsar-2.10.6/ || exit
  bin/pulsar-daemon start bookie
  echo "验证是否启动成功"
  echo "$(bin/bookkeeper shell bookiesanity)"
}

function pulsar_a() {
  initialize_cluster_metadata_a
  start_bookie
}

function pulsar_start_bookie() {
  start_bookie
}

function pulsar_b() {
  initialize_cluster_metadata_b
  start_bookie
}

# 如何使用
case $1 in
1)
  pulsar_a
  ;;
2)
  pulsar_start_bookie
  ;;
3)
  pulsar_b
  ;;
*)
  echo "pulsar集群初始化数据并启动bookie $0 1(集群a主节点初始化数据)/2(从节点启动bookie)/3(集群b主节点初始化数据)"
  ;;
esac
